package zorg.Project;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class Maps extends MapActivity  
{    
	LocationManager lm;
	LocationListener ll;
	public Locate locateclass;
	MapView mijnmapview;
	GeoPoint p;
	mijnMapOverlay itemizedOverlay;
    List<Overlay> mapOverlays;
    GeoPoint point;
    OverlayItem overlayitem;
    Drawable drawable;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        MyProperties.getInstance().MapsClass=this;
        setContentView(R.layout.maps);
        mijnmapview = (MapView) findViewById(R.id.mapView);
        mijnmapview.setBuiltInZoomControls(true);
		MapController mapController = mijnmapview.getController();
		mapController.setZoom(10);
		
		lm = (LocationManager)getSystemService(LOCATION_SERVICE);
		ll = new LocationListener() {
		 public void onLocationChanged(Location location) {
		   updatePositionOnScreen(location);
		 }
		 public void onProviderDisabled(String provider) {
		 }
		 public void onProviderEnabled(String provider) {
		 }
		 public void onStatusChanged(String provider, int status, Bundle extras) {
		 }
		};
		updatePositionOnScreen(lm.getLastKnownLocation(LOCATION_SERVICE));
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
		mapOverlays = mijnmapview.getOverlays();
		drawable = this.getResources().getDrawable(R.drawable.apartment);
	    itemizedOverlay = new mijnMapOverlay(drawable);
	    //drawObject(23.77,20.64);
	    //drawObject(22.65,-27.78);
    }
    public void drawObject(double lng, double lat,String naam) {
    	Double latitude = lat*1E6;
        Double longitude = lng*1E6;
	    point = new GeoPoint(latitude.intValue(),longitude.intValue());
	    overlayitem = new OverlayItem(point, naam, naam);
	    itemizedOverlay.addOverlay(overlayitem);
	    mapOverlays.add(itemizedOverlay);
    }
    public void updatePositionOnScreen(Location loc)
    {
    	lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
    	
    	//Location location = new Location("gps");
        //location.setLatitude(51.917889);
        //location.setLongitude(4.45766);
        
    	SetLocation(loc);
    	Locate LocState = MyProperties.getInstance().LocateClass;	
    	if (LocState!=null)
    		LocState.updatePositionOnScreen(loc);
    }
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    public void SetLocation(Location loc)
    {
    	try {
    		MapView mapView = (MapView) findViewById(R.id.mapView);
    		MapController mc = mapView.getController();
    		p = new GeoPoint((int) (loc.getLatitude() * 1E6),
    		           (int) (loc.getLongitude() * 1E6));
    		mc.animateTo(p);
    		mc.setZoom(17);  
    		mapView.invalidate();
    	} catch (Exception e) {
    		// TODO: handle exception
    	}
    }
    

    
}